package cn.itcast.nems.studentrecord.util;

import cn.itcast.nems.studentrecord.dao.dataobject.StudentRecordDO;
import cn.itcast.nems.studentrecord.dto.StudentRecordDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.NonNull;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 学籍service工具类
 *
 * @author ChangLu.Gao
 * @version v1.0
 * @program nems-server
 * @description
 * @date 2023/11/15 15:05
 */
public class StudentRecordServiceUtil {

    /**
     * 用于计算金额转化的分母
     */
    private final static BigDecimal DENOMINATOR = new BigDecimal("100");

    /**
     * db层返回的数据转化用于接口传递
     *
     * @param source 源数据
     * @return 用于传递的数据
     */
    public static StudentRecordDTO convert(@NonNull StudentRecordDO source) {
        StudentRecordDTO target = new StudentRecordDTO();
        BeanUtils.copyProperties(source, target);
        if ("MAN".equals(source.getGender())) {
            target.setGender("男");
        } else if ("WOMEN".equals(source.getGender())) {
            target.setGender("女");
        } else {
            target.setGender("未知");
        }
        target.setAmountDue(BigDecimal.valueOf(source.getAmountDue()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP).doubleValue());
        target.setActualAmountDue(BigDecimal.valueOf(source.getActualAmountDue()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        if (source.getTransferFromAmount() != null) {
            target.setTransferFromAmount(BigDecimal.valueOf(source.getTransferFromAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        }
        target.setPaidAmount(BigDecimal.valueOf(source.getPaidAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP).doubleValue());
        if (source.getBalanceAmountDue() != null) {
            target.setPayableBalance(BigDecimal.valueOf(source.getBalanceAmountDue()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP).doubleValue());
        }
        if (source.getDiscountAmount() != null) {
            target.setDiscountAmount(BigDecimal.valueOf(source.getDiscountAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP).doubleValue());
        }
        if (source.getRefundAmount() != null) {
            target.setRefundAmount(BigDecimal.valueOf(source.getRefundAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP).doubleValue());
        }
        if (source.getTransferOutAmount() != null) {
            target.setTransferOutAmount(BigDecimal.valueOf(source.getTransferOutAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP).doubleValue());
        }
        target.setPrice(BigDecimal.valueOf(source.getPrice()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        if (source.getInDate() != null) {
            target.setInDate(source.getInDate().toLocalDate());
        }
        return target;
    }
}
